package xin.marcher.wind.migrate.component.lock;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 锁竞争类对象
 */
public class PutBinlogLock {

    private final AtomicBoolean putMessageSpinLock = new AtomicBoolean(true);

    /**
     * 加锁
     */
    public void lock() {
        boolean flag;
        do {
            // 多个线程都会去进行 CAS 操作，只有一个可以把 true 变为 false, 其它线程自旋
            flag = this.putMessageSpinLock.compareAndSet(true, false);
        }
        while (!flag);
    }

    /**
     * 解锁
     */
    public void unlock() {
        this.putMessageSpinLock.compareAndSet(false, true);
    }
}
